//
//  LFBProductView.m
//  LFBLoveFreshBee
//
//  Created by Cain on 16/8/20.
//  Copyright © 2016年 丁鼎. All rights reserved.
//

#import "LFBProductView.h"
@interface LFBProductView()

@property (nonatomic , weak)UIImageView *imageView;

@property (nonatomic , weak)UIButton *button;

@property (nonatomic , strong)UIImageView *tempImageView;

@property (nonatomic , strong)NSMutableArray *animationArray;

@end

@implementation LFBProductView

- (void)setImageName:(NSString *)imageName
{
    _imageName = imageName;
    _imageView.image = [UIImage imageNamed:imageName];
}

- (instancetype)initWithFrame:(CGRect)frame{
    if (self = [super initWithFrame:frame]) {
        [self setupUI];
    }
    return self;
}

- (void)setupUI
{
    UIImageView *imageView = [[UIImageView alloc]init];
    _imageView = imageView;
    [self addSubview:imageView];
    
    UIButton *button = [[UIButton alloc]init];
    _button = button;
    [button setImage:[UIImage imageNamed:@"v2_increase"] forState:UIControlStateNormal];
    [button addTarget:self action:@selector(actionClick) forControlEvents:UIControlEventTouchUpInside];
    [self addSubview:button];
}

- (void)layoutSubviews
{
    [super layoutSubviews];
    
    [_button mas_makeConstraints:^(MASConstraintMaker *make) {
        make.trailing.bottom.offset(-5);
        make.width.height.mas_equalTo(30);
    }];
    
    [_imageView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.leading.trailing.top.mas_equalTo(self);
        make.bottom.mas_equalTo(_button.mas_top);
    }];
}

- (void)actionClick
{
    self.tempImageView = [UIImageView new];
    _tempImageView.frame = _imageView.frame;
    _tempImageView.layer.contents = _imageView.layer.contents;
    [self.layer addSublayer:_tempImageView.layer];
    
    //旋转动画
    CABasicAnimation *rotation = [CABasicAnimation animationWithKeyPath:@"transform.rotation.z"];
    rotation.toValue = [NSNumber numberWithFloat:M_PI*10];
    
    //位移动画
    CAKeyframeAnimation *translate = [CAKeyframeAnimation animationWithKeyPath:@"position"];
    
    if (self.x > KScreenWidth/2) {
        translate.values = @[
                             [NSValue valueWithCGPoint:CGPointMake(_imageView.x, _imageView.y)],
                             [NSValue valueWithCGPoint:CGPointMake(-200, 50)],
                             [NSValue valueWithCGPoint:CGPointMake(220, 400)],
                             ];
    }else{
        translate.values = @[
                             [NSValue valueWithCGPoint:CGPointMake(_imageView.x, _imageView.y)],
                             [NSValue valueWithCGPoint:CGPointMake(200, 50)],
                             [NSValue valueWithCGPoint:CGPointMake(220, 400)],
                             ];
    }
    
    
    //缩放动画
    CAKeyframeAnimation *scale = [CAKeyframeAnimation animationWithKeyPath:@"transform.scale"];
    scale.values = @[
                     [NSValue valueWithCGSize:CGSizeMake(0.7, 0.7)],
                     [NSValue valueWithCGSize:CGSizeMake(0.5, 0.5)],
                     [NSValue valueWithCGSize:CGSizeMake(0.3, 0.3)],
                     ];
    
    CAAnimationGroup *group = [CAAnimationGroup animation];
    
    group.animations = @[rotation,translate,scale];
    group.duration = 1;
    
    group.removedOnCompletion = YES;
    group.fillMode = kCAFillModeForwards;
    
    group.delegate = self;
    
    [_tempImageView.layer addAnimation:group forKey:nil];
}

- (void)animationDidStop:(CAAnimation *)anim finished:(BOOL)flag
{
//    [_tempImageView.layer removeAllAnimations];
//    [_tempImageView.layer removeFromSuperlayer];
//    [_tempImageView removeFromSuperview];
    
    NSLog(@"%@",anim);
    NSLog(@"%@",_tempImageView.layer);
    [[NSNotificationCenter defaultCenter] postNotificationName:ADD_PRODUCTNUMBER object:nil];
}

@end
